<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * 登录控制器
 */
class Login extends MY_Controller
{

    /**
     * 显示登录框
     */
    public function index()
    {
        $data = array( );

        //登录表单
        $loginForm = Modules::run( 'LoginForm' );
        $data['loginForm'] = $loginForm;

        $this->get_common_section( $data );
        $this->load->view( 'login', $data );
    }

    /**
     * 处理登录数据
     */
    public function submit()
    {
        if ( $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            //合法登录
            $loginData = $this->_getLoginData();
            $userData = $this->model_user_valid( $loginData['username'], $loginData['password'] );
            if ( $userData )
            {
                //记录到session的数据
                $userSessionData = array(
                    'id' => $userData['id'],
                    'username' => $userData['name'],
                    'email' => $userData['email'],
                    'groupid' => $userData['groupid'],
                    'groupname' => $userData['groupname'],
                );
                //记录到session中
                $this->session->setUserSession( $userSessionData );

                //提示注册成功
                $messageData = array(
                    'title' => '登录成功',
                    'message' => "欢迎{$userData['name']}回来，4秒后自动跳转回首页",
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => config_item( 'base_url' ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['loginForm'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'login', $data );
            }
            else
            {
                //登录失败 用户名或密码有误
                $data = array(
                    'errLoginTip' => '<p>请检查用户名与密码是否正确</p>',
                );
                $loginForm = Modules::run( 'LoginForm', $data );
                $data['loginForm'] = $loginForm;

                $this->get_common_section( $data );
                $this->load->view( 'login', $data );
            }
        }
        else
        {
            $this->index();
        }
    }

    /**
     * 获取登录数据
     */
    private function _getLoginData()
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        $this->form_validation->set_rules( 'username', '用户名', 'required' );
        $this->form_validation->set_rules( 'password', '密码', 'required' );
        if ( $this->form_validation->run() )
        {
            $data = array(
                'username' => $this->input->post( 'username' ),
                'password' => md5( $this->input->post( 'password' ) ),
            );
            return $data;
        }
        return false;
    }

}